import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy} = window
// ===================================================================== private component
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '授权者名称',		name: 'auth_name' },
			{ label: '授权类型',			name: 'auth_type',	type: 'select', loading:true, idStr:'code'  },
			{ label: '流程名称',			name: 'process_id',	type: 'select', loading:true },
			{ label: '节点名称 ',		name: 'node_id',	type: 'select', loading:true },
		],
		// 提交表单列表
		formData : [
			{ label: '授权类型',		name: 'auth_type', 		type: 'select',		half:true, 	required:true, 		idStr:'code', onValue:({ value })=>{
				this.getModalSelect(value)
				this.formRef.reset(['auth_id'])
			}},
			{ label: '选择节点',		name: 'node_id', 		type: 'select',		half:true,	required:true, loading:true },
			{ label: '被授权者',		name: 'auth_id', 		type: 'select', 	half:true,	required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.uuid = $fn.query('uuid')
		if(this.uuid){ this.query = { node_id: this.uuid } }
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 节点名称
			await f.getCache({ api:'node_name' } ).then(data=>{
				$fn.setRows([ forms, formData ], 'node_id', data)
				if(this.searchFormRef && this.uuid){
					this.searchFormRef.setValue({ node_id: this.uuid  })
					this.formRef.setValue({ node_id: this.uuid  })
				}
			})
			// 流程名称
			$fn.setRow(forms, 'process_id', await f.getCache({ api:'process_name' } ) )
			// 授权类型
			$fn.setRows([ forms, formData ], 'auth_type', await f.getDicCache({ code:43000 } ) )
			//
			this.setState({ forms })
		})
		
	}
	// 弹窗下拉框切换，动态加载数据
	getModalSelect = value => {
		const { formData } = this.state
		return $cache.then( async f=>{
			$fn.setRow(formData, 'auth_id', await f.getCache({ api:'be_authorized', param:{auth_type: value}, refresh:true } ) )
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'fl-process-auth/index')
	
	// 表单
	cols = [
		{ title: '授权者名称', 	dataIndex: 'auth_name', 		width:150 },
		{ title: '授权类型', 	dataIndex: 'auth_type_name', 	width:200 },
		{ title: '创建时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '所属流程', 	dataIndex: 'process_name', 		width:220 },
		{ title: '所属节点', 	dataIndex: 'node_name', 		width:220 },
		TableControls.controls.call(this,{ width: 200 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, auth_type, id }) => {
		return [
			TableControls.edit.call(this, 'fl-process-auth/info', { uuid }, {
				select: async ()=>{
					await this.getModalSelect(auth_type)
				}
			}), 			// 编辑
			this.onDel([ uuid ]),															// 删除
			TableControls.pushTo.call(this, '权限关联',  `permissions?route=fl-process-auth&uuid=${uuid}` )
		]
	}
	
	// 操作按钮
	controls = () => [
		Controls.add.call(this,{click:this.getModalSelect}),
		this.onDel(),						// 删除
		this.uuid ? Controls.back.call(this) : null
	]
	// 删除
	onDel = uuid => Delete.call(this, 'fl-process-auth/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='流程授权列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s6', api: 'fl-process-auth/add', editApi: 'fl-process-auth/edit'}) }
			</>
		)
	}
}

export default Index